import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';
import { fetchValidation } from '@/utils';
import {
  queryCabinetManagementList,
  queryCabHWareVersion,
  queryGardenList,
  operDisable,
  queryLongConfig,
  operLongLinkConfig,
  operactionSeting,
  operationCabreload,
} from './service';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetch: Effect;
    fetchCabHWare: Effect;
    fetchGarden: Effect;
    disable: Effect;
    fetchLongConfig: Effect;
    longLink: Effect;
    operactionSet: Effect;
    reloadCab: Effect;
  };
  reducers: {
    save: Reducer;
    saveCabHWare: Reducer;
    saveGarden: Reducer;
    changeLongLink: Reducer;
    clearGarden: Reducer;
  };
}

const stateInfo = {
  dataInfo: {
    list: [],
    count: 0,
  },
  cabHWareList: [],
  gardenList: [],
  showLongLink: false,
  longConfig: {},
};

const Model: ModelType = {
  namespace: 'cabinetManagementList',

  state: stateInfo,

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(queryCabinetManagementList, payload);
      yield put({
        type: 'save',
        payload: response.body,
      });
    },

    * fetchCabHWare({ payload }, { call, put }) {
      const response = yield call(queryCabHWareVersion);
      const data = fetchValidation(response);
      yield put({
        type: 'saveCabHWare',
        payload: Object.keys(data.body),
      });
    },

    * fetchGarden({ payload }, { call, put }) {
      const response = yield call(queryGardenList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'saveGarden',
        payload: data.body && data.body.list ? data.body.list : [],
      });
    },

    * disable({ payload: { search, ...query } }, { call, put }) {
      const response = yield call(operDisable, query);
      const data = fetchValidation(response);
      if (data.code === 0) {
        message.success('禁用成功');
        yield put({
          type: 'fetch',
          payload: search,
        });
      }
    },

    * fetchLongConfig({ payload: { code, change } }, { call, put }) {
      const response = yield code && call(queryLongConfig, { code });
      yield put({
        type: 'changeLongLink',
        payload: {
          data: response ? response.body : { body: {} },
          change,
        },
      });
    },

    * longLink({ payload }, { call, put }) {
      const response = yield call(operLongLinkConfig, payload);
      const data = fetchValidation(response);
      if (data) {
        message.success('保存成功');
        yield put({
          type: 'changeLongLink',
          payload: { data: { body: {} }, change: false },
        });
      }
    },

    * operactionSet({ payload: { search, ...query } }, { call, put }) {
      const response = yield call(operactionSeting, query);
      const data = fetchValidation(response);
      if (data) {
        message.success('设置成功');
        yield put({
          type: 'fetch',
          payload: search,
        });
      }
    },

    * reloadCab({ payload: { search, ...query } }, { call, put }) {
      const response = yield call(operationCabreload, query);
      const data = fetchValidation(response);
      if (data) {
        message.success('重启成功');
        yield put({
          type: 'fetch',
          payload: search,
        });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        dataInfo: action.payload,
      };
    },

    saveCabHWare(state, action) {
      return {
        ...state,
        cabHWareList: action.payload,
      };
    },

    saveGarden(state, { payload }) {
      return {
        ...state,
        gardenList: payload,
      };
    },

    changeLongLink(state, { payload }) {
      return {
        ...state,
        showLongLink: payload.change,
        longConfig: payload.data,
      };
    },

    clearGarden(state) {
      return { ...state, gardenList: [] };
    },
  },
};

export default Model;
